﻿
$.extend($.fn.validatebox.defaults.rules, {
    telNum: { //既验证手机号，又验证座机号
        validator: function (value, param) {
            return /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\d3)|(\d{3}\-))?(1[34578]\d{9})$)/.test(value);
        },
        message: '请输入正确的电话号码。'
    },
    starteEail: { //验证email前缀
        validator: function (value, param) {
            return /(^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]$)/.test(value);
        },
        message: '请输入正确的邮箱地址。'
    }
});

/*布局部分*/
$('#theme-login-layout').layout({
    fit: true /*布局框架全屏*/
});

/*表单定义*/
$('#usertypeselect').combobox({
    editable: false, /*不允许用户通过输入选择*/
    onChange: function (newValue, oldValue) {
        $("#usertype").val(newValue);
    }
});

var usertypeselect = $('#usertypeselect').combobox('panel');
usertypeselect.panel({ cls: "usertypeselect-panel" });

$('#email').textbox({
    prompt: '邮箱',
    required: true,
    missingMessage: "请输入邮箱名称"
});

$('#emailselect').combobox({
    editable: false
});

var mailselect = $('#emailselect').combobox('panel');
mailselect.panel({ cls: "theme-mail-select-panel" });

$('#username').textbox({
    prompt: '用户名',
    required: true,
    missingMessage: "请输入用户名"
});

$('#passwords').textbox({
    type: "密码",
    prompt: '密码',
    required: true,
    missingMessage: "请输入密码"
});

$('#codes').textbox({
    required: true,
    missingMessage: "请输入验证码",
    buttonText: '重新获取验证码',
    prompt: '验证码',
    onClickButton: function () {
        ChangeImage();
    }
});

$('#login').form({
    url: "/EasyUI/Admin/CheckLogin",
    onSubmit: function () {
        if ($("#usertype").val() === "") {
            $.messager.alert('温馨提示', "请选择用户类型！", 'error');
            return false;
        }
        var res = $(this).form('enableValidation').form('validate');
        if (res) {
            $("#useremail").val($("#email").val() + $("#emailselect").find("option:selected").text());
            $.messager.progress();
        }
        return res;
    },
    success: function (result) {
        debugger;
        if (typeof (result) !== "object")
            result = JSON.parse(result);
        $.messager.progress('close');
        if (result !== null && typeof (result) === "object") {
            if (result.code === 200) {
                OperateMsg("授权提示", result.msg);
                window.location.href = result.url;
            }
            else if (result.code === 401)
                $.messager.alert('授权提示', result.msg, 'error');
            else if (result.code === 402) {
                ChangeImage();
                $.messager.alert('异常提醒', result.msg, 'error');
            }
            else
                $.messager.alert('异常提醒', '系统异常', 'error');
        }
        else
            $.messager.alert('异常提醒', '系统异常', 'error');
    },
    error: function () {
        $.messager.alert('异常错误', '异常错误', 'error');
    }
});

//刷新验证码
function ChangeImage() {
    var code = $("#img").attr("src");
    $("#img").attr("src", code + "1");
}
